//
//  UICollectionView+WSExtension.m
//  Component
//
//  Created by 王诗文 on 2018/7/26.
//  Copyright © 2018年 王诗文. All rights reserved.
//

#import "UICollectionView+WSExtension.h"
#import <objc/runtime.h>

@implementation UICollectionViewCell (WSExtension)
- (void)wsSubViewsClickedData:(id)data{
     [self.wsDelegate wsCellSubViewsClickedIndexPath:self.indexPath data:data];
}

- (void)setWsDelegate:(id<WSCollectCellSubViewsActionDelegate>)wsDelegate{
     objc_setAssociatedObject(self, @selector(wsDelegate), wsDelegate, OBJC_ASSOCIATION_ASSIGN);
}

- (id<WSCollectCellSubViewsActionDelegate>)wsDelegate{
     return objc_getAssociatedObject(self, _cmd);
}

- (NSIndexPath *)indexPath{
     return objc_getAssociatedObject(self, _cmd);
}
- (void)setIndexPath:(NSIndexPath *)indexPath{
     objc_setAssociatedObject(self, @selector(indexPath), indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)attachObj{
     return objc_getAssociatedObject(self, _cmd);
}
- (void)setAttachObj:(id)attachObj{
     objc_setAssociatedObject(self, @selector(attachObj), attachObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end



@implementation UICollectionView (WSExtension)

- (void)wsRegisterCellName:(NSString *)name{
    if (!name) return;
     NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"nib"];
    if (path) {
        [self registerNib:[UINib nibWithNibName:name bundle:nil] forCellWithReuseIdentifier:name];
    }else{
        [self registerClass:NSClassFromString(name) forCellWithReuseIdentifier:name];
    }
}

- (void)wsRegisterHeaderName:(NSString *)name{
    if (!name) return;
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"nib"];
    if (path) {
        [self registerNib:[UINib nibWithNibName:name bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:name];
    }else{
        [self registerClass:NSClassFromString(name) forCellWithReuseIdentifier:name];
    }
}

- (void)wsRegisterFooterName:(NSString *)name{
    if (!name) return;
    UINib *nib = [UINib nibWithNibName:name bundle:nil];
    if (nib) {
        [self registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:name];
    }else{
        [self registerClass:NSClassFromString(name) forCellWithReuseIdentifier:name];
    }
}

@end
